﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace HotUpdateDLL
{
    /// <summary>
    /// 资源信息
    /// </summary>
    public class AssetInfo
    {

        #region AssetInfo Fields (字段)

        private UnityEngine.Object _object;  // 实例对象
        public Type AssetType; // 资源类型
        public string Puth; // 路径
        public int RefCount; // 参考长度

        #endregion

        #region LsLoaded & AssetObect Attribute (属性)
        /// <summary>
        /// 是否正在加载
        /// </summary>
        public bool IsLoaded
        {
            get
            {
                return null != _object;
            }
        }

        /// <summary>
        /// 资源对象
        /// </summary>
        public UnityEngine.Object AssetObject
        {
            get
            {
                if (_object == null)
                    _object = Resources.Load(Puth); // 测试阶段 使用Resources 完成后 使用AssetBundle
                return _object;
            }
        }
        #endregion

        #region AsyncLoad & Load Funcetion (异步加载和加载方法)

        /// <summary>
        /// 及时加载
        /// </summary>
        /// <param name="_loaded"></param>
        /// <returns></returns>
        public IEnumerator GetCoroutineObject(Action<UnityEngine.Object> _loaded)
        {
            while (true)
            {
                yield return null;
                if (_object == null)
                {
                    ResourcesLoad();
                    yield return null;
                }
                if (_loaded == null)
                {
                    _loaded(_object);
                }
                yield break;
            }
        }

        /// <summary>
        /// 资源加载
        /// </summary>
        private void ResourcesLoad()
        {
            try
            {
                _object = Resources.Load(Puth);
                if (null == _object)
                {
                    Debug.LogError($"Resources Load Failure! Path:{Puth}");
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e.ToString());
                throw;
            }

        }

        /// <summary>
        /// 异步加载
        /// </summary>
        /// <param name="_Loaded"></param>
        /// <param name="_progress"></param>
        public IEnumerator GetAsyncLoad(Action<UnityEngine.Object> _Loaded ,Action<float> _progress)
        {
            if (_object != null)
            {
                _Loaded(_object);
            }

            ResourceRequest resourceRequest = Resources.LoadAsync(Puth);

            while (resourceRequest.progress <0.9)
            {
                if (null != _progress)
                {
                    _progress(resourceRequest.progress);
                    yield return null;
                }
            }

            while ( !resourceRequest.isDone)
            {
                _progress?.Invoke(resourceRequest.progress);
                yield return null;
            }

            _object = resourceRequest.asset;

            _Loaded?.Invoke(_object);
            yield return resourceRequest;

        }


        #endregion
    }



    public class ResourcesManage : Singleton<ReferenceManage>
    {

        #region 加载 load References(资源) & Instantiate object (实例对象)

        #endregion

        #region Load References(加载资源)

        #endregion

        #region Load Coroutine References (协程加载资源)

        #endregion

        #region Load Async References(异步加载资源)

        #endregion

        #region Get AssetInfo & Instantiate object (返回异步信息 和 资源对象)

        #endregion

    }
}
